// pages/show/show.js
import { request } from "../../request/index.js";
var app = getApp();
let a = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicInfo: {},
    discussInfo: {},
    topicid: '',
    allDiscuss: {},
    MyCollectedTopic: [],
    isCollect: '',
    collectTopic: {},
    isfocus: false,
    isLongpress: false,
    uid: '',
    currentid: ''

  },



  onShow: function () {
    if (a) {
      a = false;
      return
    } else {
      let pages = getCurrentPages();
      let currentPage = pages[pages.length - 1];
      let options = currentPage.options;
      const uid = app.globalData.uid
      const { id } = options;
      this.setData({
        topicid: id,
        collectTopic: {
          tid: id,
          uid: app.globalData.uid
        }
      })
      this.getTopicDetail(id);
      this.getCollectTopicByUid(uid);
      this.getTopicReplyAll(id)

    }


  },

  longpress(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    that.setData({
      isLongpress: true,
      currentid: id
    })
    wx.showActionSheet({
      itemList: ['删除评论'],
      itemColor: '#000000',
      success: (result) => {
        that.deleteReply(id)
      },
      complete: () => {
        that.setData({
          isLongpress: false
        })
      }
    });
  },
  async deleteReply(id) {
    var that = this
    const res = await request({ url: "/topicReply/deleteReplyById", data: { id } })
    var id = that.data.topicid
    that.getTopicReplyAll(id);
  },
  async getTopicDetail(id) {
    var that = this
    const res = await request({ url: "/topic/getTopicById", data: { id } })
    that.setData({
      topicInfo: res
    })
  },
  async getCollectTopicByUid(uid) {
    var that = this
    console.log(uid)
    console.log(typeof(uid))
    const res = await request({ url: "/collecttopic/getCollectTopicByUid", data: { uid } })
    that.setData({
      MyCollectedTopic: res,
    })
    var MyCollectedTopic = that.data.MyCollectedTopic
    var topicInfo = that.data.topicInfo
    // 判断贴子对象是否存在我的收藏数组中
    let index = MyCollectedTopic.findIndex(v => v.tid === that.data.topicInfo[0].id);
    if (index !== -1) {
      that.setData({
        isCollect: true
      })
    } else {
      that.setData({
        isCollect: false
      })
    }
  },

  handleImagePreview: function (e) {
    a = true
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },
  //获取输入的评论值
  discussInput: function (e) {
    var that = this
    that.setData({
      discussInfo: {
        replycontent: e.detail.value,
        replytopicid: that.data.topicid,
        replyuserid: app.globalData.uid

      }


    })
  },
  async discussSend() {
    var that = this
    var discussInfo = that.data.discussInfo
    var content = that.data.discussInfo.replycontent
    var id = that.data.discussInfo.replytopicid
    if (!content) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000,
        mask: false,
      });
      return false;

    } else {
      const res = await request({ url: "/topicReply/addTopicReply", method: "POST", data: discussInfo })
      that.setData({
        discussInfo: ''
      })
      that.getTopicReplyAll(id);
    }
  },
  async getTopicReplyAll(id) {
    var that = this
    const res = await request({ url: "/topicReply/getTopicReplyById", data: { id } })
    that.setData({
      allDiscuss: res
    })
  },
  async handlecollect() {
    var that = this
    const uid = app.globalData.uid
    var MyCollectedTopic = that.data.MyCollectedTopic
    var goodsInfo = that.data.goodsInfo
    if (that.data.isCollect === false) {
      var collectTopic = that.data.collectTopic
      const res = await request({ url: "/collecttopic/addCollectTopic", method: "POST", data: collectTopic })
      that.getCollectTopicByUid(uid)

    } else {
      var tid = that.data.topicid
      const res1 = await request({ url: "/collecttopic/deleteCollectTopicById", data: { tid, uid } })
      that.getCollectTopicByUid(uid)
    }
  },
  showInput() {
    this.setData({
      isfocus: true
    })
  }


})